package com.meng.menggaga.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.meng.menggaga.R;
import com.meng.menggaga.util.CommonParameter;
import com.nineoldandroids.animation.Animator;

/**
 * 启动页
 * 
 * 后期会增加活动宣传模式的启动页
 * 
 * @author simba
 * 
 */
public class SplashActivity extends Activity {
    private View mTarget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mTarget = findViewById(R.id.icon);

        int id = ((int) (Math.random() * 6));
        switch (id) {
            case 0:
                CommonParameter.sStyleBgColor = R.color.style_blue;
                break;
            case 1:
                CommonParameter.sStyleBgColor = R.color.style_green;
                break;
            case 2:
                CommonParameter.sStyleBgColor = R.color.style_pink;
                break;
            case 3:
                CommonParameter.sStyleBgColor = R.color.style_pink_light;
                break;
            case 4:
                CommonParameter.sStyleBgColor = R.color.style_red;
                break;
            case 5:
                CommonParameter.sStyleBgColor = R.color.style_yellow;
                break;

            default:
                CommonParameter.sStyleBgColor = R.color.black;
                break;
        }

        CommonParameter.sStyleBgColor = R.color.style_blue;

        findViewById(R.id.container_bg).setBackgroundColor(
                getResources().getColor(CommonParameter.sStyleBgColor));

        // FrameLayout activityRoot = (FrameLayout) findViewById(android.R.id.content);
        // View parent = activityRoot.getChildAt(0);
        //
        // // better way ?
        // mAnimated = new AnimatedDoorLayout(this);
        // activityRoot.removeView(parent);
        // activityRoot.addView(mAnimated, parent.getLayoutParams());
        // mAnimated.addView(parent);

        loaded();
    }

    private void loaded() {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                YoYo.with(Techniques.values()[(int) (Math.random() * Techniques.values().length)])
                        .duration(1000).interpolate(new AccelerateDecelerateInterpolator())
                        .withListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                                mTarget.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                                if (!isFinishing()) {
                                    startActivity(new Intent(getBaseContext(), MainActivity.class));
                                    finish();
                                    overridePendingTransition(R.anim.in_from_right,
                                            R.anim.out_from_left);
                                }
                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {
                                Toast.makeText(getBaseContext(), "canceled", Toast.LENGTH_SHORT)
                                        .show();
                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {}
                        }).playOn(mTarget);
            }
        }, 300);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

}
